<!-- #layout name=empty -->
<div class="container">
    <div class="row">
        <div class="col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4">
            <div class="block-small-form">
                <div class="logo">
                    <img src="/_Admin/Images/logo-blue.png" alt="Logo">
                </div>
                <form data-bind="submit: onSubmit">
                    <div class="form-group">
                        <label class="sr-only">New password</label>
                        <input class="form-control" type="password" data-bind="value: newPassword, error: newPassword" placeholder="New password" />
                    </div>
                    <div class="form-group">
                        <label class="sr-only">Confirm password</label>
                        <input class="form-control" type="password" data-bind="value: confirmPassword, error: confirmPassword" placeholder="Confirm password" />
                    </div>
                    <button class="btn blue pull-right" type="submit">Reset</button>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    $(function() {

        var resetPasswordVM = function() {
            var self = this;

            this.isValid = function() {
                return self.newPassword.isValid() && self.confirmPassword.isValid()
            }

            this.onSubmit = function() {
                if (self.isValid()) {
                    Kooboo.User.ResetPassword({
                        token: Kooboo.getQueryString("token"),
                        password: self.newPassword()
                    }).then(function(res) {

                        if (res.success) {
                            location.href = Kooboo.Route.Get(Kooboo.Route.User.LoginPage);
                        }
                    })
                } else {
                    self.showError(true);
                }
            }

            this.showError = ko.observable(false);

            this.newPassword = ko.validateField({
                required: Kooboo.text.validation.required
            })
            self.newPassword.subscribe(function() {
                self.confirmPassword.valueHasMutated();
            })

            this.confirmPassword = ko.validateField({
                required: Kooboo.text.validation.required,
                equals: {
                    value: function() {
                        return self.newPassword()
                    },
                    message: Kooboo.text.validation.notEqual
                }
            })

            this.loginPageUrl = Kooboo.Route.Get(Kooboo.Route.User.LoginPage);
        }

        var rp = new resetPasswordVM();
        ko.applyBindings(rp, document.getElementsByClassName("container")[0]);
    });
</script>